package com.llkj.qingnianguoxue.course;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.llkj.qingnianguoxue.R;
import com.llkj.qingnianguoxue.adapter.BookCourseDateAdapter;
import com.llkj.qingnianguoxue.adapter.BookingCourseAdapter;
import com.llkj.qingnianguoxue.base.BaseActivity;
import com.llkj.qingnianguoxue.bean.BookingCourseBean;
import com.llkj.qingnianguoxue.bean.CourseTeacherBean;
import com.llkj.qingnianguoxue.bean.RxCoursePayBean;
import com.llkj.qingnianguoxue.bean.TearcherDetailBean;
import com.llkj.qingnianguoxue.bean.WeekDateBean;
import com.llkj.qingnianguoxue.bean.response.ResponseList;
import com.llkj.qingnianguoxue.http.HttpMethod;
import com.llkj.qingnianguoxue.util.DisplayUtil;
import com.llkj.qingnianguoxue.util.GridSpacingItemDecoration;
import com.llkj.qingnianguoxue.util.GsonUtil;
import com.llkj.qingnianguoxue.util.ImageLoadingUtil;
import com.llkj.qingnianguoxue.util.RxBus;
import com.llkj.qingnianguoxue.util.helper.LoadDataHelper;

import java.util.ArrayList;
import java.util.List;

import rx.functions.Action1;

public class TeacherDetailActivity extends BaseActivity implements BaseQuickAdapter.RequestLoadMoreListener,
        LoadDataHelper.LoadMoreListener, SwipeRefreshLayout.OnRefreshListener {
    private ImageView mIvBg,mIvHead;
    private TextView mUserName;
    private RecyclerView rvBookDate;
    private BookCourseDateAdapter mCourseDateAdapter;
    private List<WeekDateBean> mWeekDateData;
    private SwipeRefreshLayout refreshLayout;
    private RecyclerView lvCourseView;
    private BookingCourseAdapter mBookingAdapter;
    private List<BookingCourseBean> mBookData;
    private int totalSize,mPage;
    private String mTeacherName,mMasterId,mCurrentDate;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_teacher_detail);
        Intent intent = getIntent();
        mMasterId = intent.getStringExtra("id");
        mTeacherName = intent.getStringExtra("name");
        setTitle(mTeacherName, true, TITLE_TYPE_IMG, R.drawable.icon_back, false, -1, -1);
        initStatusbar(TSATUSBAR_TYPE_BLACK_TEXT,rlTitle,R.color.gray_bar);

        initView();
        initListener();

//        loadWaitDialog();
        HttpMethod.getCurriculumMasterDetail(this,this,mMasterId);
    }

    private void initView(){
        mIvBg = (ImageView) findViewById(R.id.iv_bg);
        mIvHead = (ImageView) findViewById(R.id.iv_head);
        mUserName = (TextView) findViewById(R.id.tv_name);
        mUserName.setText(mTeacherName);

        rvBookDate = (RecyclerView) findViewById(R.id.rvBookDate);
        rvBookDate.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL,false));

        mWeekDateData = new ArrayList<>();
        mCourseDateAdapter = new BookCourseDateAdapter(R.layout.item_booking_course_date,mWeekDateData);
        rvBookDate.setAdapter(mCourseDateAdapter);

        refreshLayout = (SwipeRefreshLayout) findViewById(R.id.refresh_layout);
        refreshLayout.setColorSchemeResources(R.color.blue3989ff);
        lvCourseView = (RecyclerView) findViewById(R.id.recyclerView);
        lvCourseView.setLayoutManager(new GridLayoutManager(this, 3));
        lvCourseView.addItemDecoration(new GridSpacingItemDecoration(3, 10, false));
        int padding = DisplayUtil.dip2px(this,10);
        lvCourseView.setPadding(padding,padding,padding,padding);

        mBookData = new ArrayList<>();
        mBookingAdapter = new BookingCourseAdapter(R.layout.item_booking_course,mBookData);
        lvCourseView.setAdapter(mBookingAdapter);

        refreshLayout.setOnRefreshListener(this);
//        mBookingAdapter.setOnLoadMoreListener(this);
    }

    private void initListener(){
        lvCourseView.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                BookingCourseBean item = mBookData.get(i);
                Bundle bundle = new Bundle();
                bundle.putString("title",item.getName());
                bundle.putString("id",item.getId());
                startActivity(CourseDetailActivity.class,bundle);
            }
        });
        rvBookDate.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                mCourseDateAdapter.setSelectPosition(i);
                mCurrentDate = mWeekDateData.get(i).getStart_time();
                onRefresh();
            }
        });

        RxBus.getInstance().toObservable(RxCoursePayBean.class).subscribe(new Action1<RxCoursePayBean>() {
            @Override
            public void call(RxCoursePayBean bean) {
                if (bean.isPaySuccess){
                    onRefresh();
                }
            }
        });
    }

    @Override
    public void onRefresh() {
        HttpMethod.getCurriculumCurriculum(this,this,mMasterId,mCurrentDate);
        refreshLayout.setRefreshing(false);
    }

    @Override
    public void onLoadMoreRequested() {
        LoadDataHelper.loadMoreRequest(this, mBookingAdapter, null, lvCourseView, totalSize);
    }

    @Override
    public void onLoadMore() {

    }

    @Override
    protected void onhttpSuccess(String data, int method) {
        super.onhttpSuccess(data, method);
        refreshLayout.setRefreshing(false);
        if (method == HttpMethod.HTTP_CURRICULUM_MASTER_DETAILS) {
            TearcherDetailBean detailBean = GsonUtil.GsonToBean(data, TearcherDetailBean.class);
            if (detailBean != null) {
                CourseTeacherBean courseTeacherBean = detailBean.getMaster();
                ImageLoadingUtil.loadingCircleBorderImg(mIvHead, courseTeacherBean.getImg(), R.color.transparent,0);
                ImageLoadingUtil.loadBlurTransImg(mIvBg, courseTeacherBean.getBg_img());

                mWeekDateData.addAll(detailBean.getWeek());
                mCourseDateAdapter.notifyDataSetChanged();

                WeekDateBean dateBean  = null;
                int selectWeek = 0;
                for(int i=0;i<mWeekDateData.size();i++){
                    dateBean = mWeekDateData.get(i);
                    if("1".equals(dateBean.getIs_xian())){
                        selectWeek = i;
                    }
                }
                if(mWeekDateData.size()>0){
                    mCourseDateAdapter.setSelectPosition(selectWeek);
                    mCurrentDate = mWeekDateData.get(selectWeek).getStart_time();
                    onRefresh();
                }

            }
        }else if(method == HttpMethod.HTTP_CURRICULUM_CURRICULUM){
            ResponseList<BookingCourseBean> responseList = GsonUtil.fromJsonList(data,BookingCourseBean.class);
            if(responseList!=null && responseList.getCurriculum()!=null){
                mBookData.clear();
                mBookData.addAll(responseList.getCurriculum());
                mBookingAdapter.setCurrentDate(mCurrentDate);
                mBookingAdapter.notifyDataSetChanged();
            }
        }
    }
}
